package com.marlowe.web.borrow;

import com.alibaba.fastjson.JSONObject;
import com.marlowe.api.constants.ResultDTO;
import com.marlowe.api.enums.HttpCode;
import com.marlowe.api.enums.ValidFlagEnum;
import com.marlowe.api.model.book.BookDTO;
import com.marlowe.api.model.borrow.BookBorrowDTO;
import com.marlowe.api.service.book.BookService;
import com.marlowe.api.service.borrow.BookBorrowService;
import com.marlowe.dao.mapper.book.BookMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.Date;

/**
 * @program: lib-web
 * @description:
 * @author: Marlowe
 * @create: 2021-01-03 12:04
 **/
@RestController
@RequestMapping("/borrow")
public class BookBorrowController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private BookBorrowService bookBorrowService;


    /**
     * 借书操作
     *
     * @param jsonObject
     * @return
     */
    @RequestMapping("/doBorrow")
    public ResultDTO doBorrow(@RequestBody JSONObject jsonObject) {
        try {
            // 1.借书的编号
            int bookId = Integer.parseInt(String.valueOf(jsonObject.getOrDefault("bookId", 0)));
            // 借书起始截止日期(日期前后非法校验交给前端来做)
            Date startDate = jsonObject.getDate("startDate");
            Date endDate = jsonObject.getDate("endDate");
            int borrowCount = jsonObject.getInteger("borrowCount");
            int userId = jsonObject.getInteger("userId");
            String userName = jsonObject.getString("userName");
            boolean vipFlag = jsonObject.getBoolean("vipFlag");

            return bookBorrowService.doBorrow(bookId, startDate, endDate, borrowCount, userId, userName, vipFlag);
        } catch (Exception e) {
            logger.error("系统异常:" + e);
            return new ResultDTO(HttpCode.ERROR.getCode(), "系统异常");
        }
    }


}
